﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;

namespace getPixelInformation
{
    class Pixels
    {
        private List<Pixel> picture;
        private int n;
        private int m;

        public void setPicture(List<Pixel> p)
        {
            int n = p.Count;
            for (int i = 0; i < n; i++)
            {
                this.picture[i] = p[i];
            }
        }

        public List<Pixel> getPicture()
        {
            return this.picture;
        }

        public void setN(int n)
        {
            this.n = n;
        }

        public int getN()
        {
            return n;
        }

        public void setM(int m)
        {
            this.m = m;
        }

        public int getM()
        {
            return m;
        }

        public Pixels()
        {
            this.picture = new List<Pixel>();
            this.m = 0;
            this.n = 0;
        }

        public Pixels(int n, int m)
        {
            this.picture = new List<Pixel>();
            this.m = m;
            this.n = n;
        }

        public Pixels(Pixels p)
        {
            this.picture = new List<Pixel>();
            int e = p.picture.Count;
            this.n = p.n;
            this.m = p.m;
            for (int i = 0; i < e; i++)
            {
                this.picture[i] = p.picture[i];
            }
        }

        public void getPixelInformation(string filePath)
        {
            Bitmap bm = new Bitmap(filePath);

            this.n = bm.Height;
            this.m = bm.Width;

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    Color C = bm.GetPixel(j,i);
                    Pixel temp = new Pixel(C.R, C.G, C.B);
                    this.picture.Add(temp);
                }
            }

        }

        public bool writePixelInFormation(string filePathR, string filePathG, string filePathB, string filePathGS)
        {
            bool result = true;

            StreamWriter sWR = new StreamWriter(filePathR);
            StreamWriter sWG = new StreamWriter(filePathG);
            StreamWriter sWB = new StreamWriter(filePathB);
            StreamWriter sWGS = new StreamWriter(filePathGS);
            try
            {
                //sWR.WriteLine(this.n.ToString() + " " + this.m.ToString());
                //sWG.WriteLine(this.n.ToString() + " " + this.m.ToString());
                //sWB.WriteLine(this.n.ToString() + " " + this.m.ToString());
                //sWGS.WriteLine(this.n.ToString() + " " + this.m.ToString());

                int vt = -1;

                for (int i = 0; i < this.n; i++)
                {
                    for (int j = 0; j < this.m; j++)
                    {
                        vt = vt + 1;
                        sWR.Write((this.picture[vt].getRed()).ToString() + " ");
                        sWG.Write((this.picture[vt].getGreen()).ToString() + " ");
                        sWB.Write((this.picture[vt].getBlue()).ToString() + " ");
                        sWGS.Write((this.picture[vt].getGrayScale()).ToString() + " ");
                    }
                    sWR.WriteLine();
                    sWG.WriteLine();
                    sWB.WriteLine();
                    sWGS.WriteLine();
                }

                sWR.Close();
                sWG.Close();
                sWB.Close();
                sWGS.Close();
            }
            catch
            {
                result = false;
                sWR.Close();
                sWG.Close();
                sWB.Close();
                sWGS.Close();
            }

            return result;
        }
    }
}
